package cn.edu.jxnu.sse.servlet;

import cn.edu.jxnu.sse.utils.Captcha;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@WebServlet(value="/code")
public class CodeServet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("image/jpeg");
        /*java.util.Date date = new java.util.Date();
        resp.setDateHeader("Last-Modified",date.getTime()); //Last-Modified:页面的最后生成时间
        resp.setDateHeader("Expires",date.getTime()+20000); //Expires:过时期限值
        resp.setHeader("Cache-Control", "public"); //Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息；
        resp.setHeader("Pragma", "Pragma"); //*/
        //设置响应头控制浏览器不要缓存
  /*      resp.setDateHeader("expries", -1);
        resp.setHeader("Cache-Control", "no-cache");
        resp.setHeader("Pragma", "no-cache");*/

        BufferedImage bi = (BufferedImage) Captcha.getImageCode(80,30).get("image");
        //将图片传给浏览器
        ImageIO.write(bi, "jpg", resp.getOutputStream());
    }
}
